package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyListObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/strategy")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IRegionService regionService;

    //获取整个列表,没有查询条件
    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo") StrategyListObject qo){
        model.addAttribute("result",strategyService.query(qo));
        model.addAttribute("regions",regionService.list());
        return "strategy/list";
    }

    //推荐游记的新增和修改
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Strategy entity, MultipartFile img) {
        //判断有没有设置封
        if (img != null && img.getSize() > 0 && img.getContentType().startsWith("image")){
            //把图片上传到本地图库,返回图片路径
            String url = UploadUtil.upload(img);
            //替换以前默认的封面
            entity.setCoverUrl(url);
        }
        strategyService.saveOrUpdate(entity);
        return "redirect:/strategy/list.do";
    }
}
